package com.tongcao.cn.oauth.client;

import com.tongcao.cn.dfire.result.Result;
import com.tongcao.cn.dfire.utils.ResultUtil;
import com.tongcao.cn.oauth.cons.ReplaceBeanName;
import com.tongcao.cn.oauth.executor.IJumpExecutor;
import com.tongcao.cn.oauth.request.JumpRequest;
import com.tongcao.cn.processor.BeanHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author:tongcao
 * @since:2019/11/22 15:36
 * Description:跳转客户端
 */
@Component
public class JumpClient implements IJumpClient {

    @Resource
    private BeanHelper beanHelper;

    @Override
    public <T> Result<T> execute(JumpRequest<T> request) {
        final String beanName = ReplaceBeanName.getName(ReplaceBeanName.REQUEST, ReplaceBeanName.EXECUTOR, request.getClass());
        Result result;
        try {
            IJumpExecutor authExecutor = beanHelper.getBean(beanName, IJumpExecutor.class);
            result = authExecutor.execute(request);
        } catch (Exception e) {
            result = ResultUtil.failResult("400", "Exception跳转出错");
        } catch (Throwable e) {
            result = ResultUtil.failResult("400", "Throwable跳转出错");
        }
        return result;
    }
}